<?php

/**
 * product actions.
 *
 * @package    emc
 * @subpackage product
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class productActions extends sfActions
{
	public function executeIndex(sfWebRequest $request)
	{
		$this->forward('default', 'module');
	}
	public function executeView(sfWebRequest $request)
	{
		// Change Language
		if (!$request->getParameter('sf_culture'))
		{
			if ($this->getUser()->isFirstRequest())
			{
				$culture = $request->getPreferredCulture(array('vi', 'en'));
				$this->getUser()->setCulture($culture);
				$this->getUser()->isFirstRequest(false);
			}
			else
			{
				$culture = $this->getUser()->getCulture();
			}
		}
		$product_id = $request->getParameter("product_id");
		$product = Doctrine_Core::getTable("Product")->find($product_id);
		$current_category = Doctrine_Core::getTable("Category")->find($product->category_id);
		
		$options = Doctrine_Query::create()
					->from("ProductOption")
					->where("product_id = ?", $product_id);
		
		$this->options = ($options->count()>0)?$options->fetchArray():NULL;
		$this->product = $product;
		$this->current_category = $current_category;
		return sfView::SUCCESS;
	}
	public function executeCheck(sfWebRequest $request)
	{
		$option_id = $request->getParameter("option_id");
		$product_id = $request->getParameter("product_id");
		
		$avail = 0;
		if ($option_id == 0)
		{
			$product = Doctrine_Core::getTable("Product")->find($product_id);
			$avail = $product->available;
		} 
		else 
		{
			$option = Doctrine_Core::getTable("ProductOption")->find($option_id);
			$avail = $option->available;
		}
		
		$this->avail = $avail;
		$this->setTemplate("check");
	  	$this->setLayout(false);
	}
}
